#include <iostream>
#include <vector>
#include <string>
#include<algorithm>
using std::string;
using std::vector;
using std::max;

class Solution
{
public:
    bool exist(vector<vector<char>> &board, string word)
    {
        vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));
        for (int i = 0; i < board.size(); i++) {
            for (int j = 0; j < board[0].size(); j++) {
                if (check(board, visited, word, 0, i, j)) return true;
            }
        }
        return false;
        
    }

    bool check(vector<vector<char>>& board, vector<vector<bool>>& visited, string& word, int matchLen, int i, int j) {
        // 检查board[i, j]可行性，并搜索之后的
        std::cout << string(30, '=') << "\n";
        std::cout << matchLen << "\n";
        std::cout << "(" << i << ", " << j << "), char " << board[i][j] << "\n";
        std::cout << string(30, '*') << "\n";
        for (auto& v: visited) {
            for (auto n: v) std::cout << n << " ";
            std::cout << "\n";
        }
        if (board[i][j] == word[matchLen] && !visited[i][j]) {
            matchLen++;
            if (matchLen == word.size()) return true;
            visited[i][j] = true;
            if (i > 0 && check(board, visited, word, matchLen, i-1, j)) return true;    // up
            if (j > 0 && check(board, visited, word, matchLen, i, j-1)) return true;    // left
            if (i < board.size()-1 && check(board, visited, word, matchLen, i+1, j)) return true; //down
            if (j < board[0].size()-1 && check(board, visited, word, matchLen, i, j+1)) return true;    //right
            matchLen--;
            visited[i][j] = false;
        }
        return false;

    }
};

int main() {
    int m, n;
    std::cin >> m >> n;
    std::cout << "shape (" << m << "," << n << ")\n";
    vector<vector<char>> input;
    char temp;
    for (int i = 0; i < m; i++) {
        input.push_back({});
        for (int j = 0; j < n; j++) {
            std::cin >> temp;
            input[i].push_back(temp);
        }
        for (auto& c : input[i]) {
            std::cout << c << " ";
        }
        std::cout << "\n";
    }
    string word;
    std::cin >> word;
    std::cout << "word: " << word << "\n";
    std::cout << "exist: \n";
    bool res = Solution().exist(input, word);
    std::cout << res << "\n";
    return 0;
}